﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Obout.Mvc.ComboBox;
using SHDQGSWEB.Models;

namespace SHDQGSWEB.Controllers
{
    public class GadgetsController : Controller
    {
        //
        // GET: /Gadgets/
        // Gadgets
        private GadgetRepository _gadgetRespository;
        private ConfigRepository _configRepository;

        protected override void Initialize(RequestContext requestContext)
        {
            // initialize Gadgets
            _configRepository = new ConfigRepository();
            _gadgetRespository = new GadgetRepository();

            // End of initialize gadgets
            base.Initialize(requestContext);
        }

        [Authorize(Roles = "admin")]
        public ActionResult Details(int gadgetId)
        {
            Gadget navigatorItem = _gadgetRespository.FindGadget(gadgetId);
            return View(navigatorItem);
        }

        /// <summary>
        /// Delete gadget
        /// </summary>
        /// <param name="gadgetId"></param>
        /// <returns></returns>
        [Authorize(Roles = "admin")]
        public ActionResult Delete(int gadgetId)
        {
            Gadget navigatorItem = _gadgetRespository.FindGadget(gadgetId);
            return View(navigatorItem);
        }

        [AcceptVerbs(HttpVerbs.Post), Authorize(Users = "admin")]
        public ActionResult Delete(Gadget gadget)
        {
            _gadgetRespository.DeleteGadget(gadget);
            _gadgetRespository.Save();
            return View("Deleted");
        }

        /// <summary>
        /// Index of Gadget
        /// </summary>
        /// <returns></returns>
        [Authorize(Roles = "admin")]
        public ActionResult Index()
        {
            var navigatorItems = _gadgetRespository.FindGadgets("NavigatorBar");
            return View(navigatorItems);
        }

        /// <summary>
        /// Edit a gadget
        /// </summary>
        /// <param name="gadgetId"></param>
        /// <returns></returns>
        [Authorize(Roles = "admin")]
        public ActionResult Edit(int gadgetId)
        {
            var navigatorItem = _gadgetRespository.FindGadget(gadgetId);
            return View(navigatorItem);
        }
        [AcceptVerbs(HttpVerbs.Post), Authorize(Roles = "admin")]
        public ActionResult Edit(int gadgetId,FormCollection formCollection)
        {
            Gadget gadget = new Gadget();
            UpdateModel(gadget);
            _gadgetRespository.Save();
            return RedirectToAction("Details", new { gadgetId = gadget.GadgetID });
        }
        /// <summary>
        /// Create a gadget
        /// </summary>
        /// <returns></returns>
        [Authorize(Roles = "admin")]
        public ActionResult Create()
        {
            var gadget = new Gadget();
            var values = _configRepository.GetConfigurationValues("Gadgets","Create","TypeCombobox");
            ViewData["TypeCombobox"] = new ComboBoxItemList(values);
            return View(gadget);
        }
        [AcceptVerbs(HttpVerbs.Post), Authorize(Roles = "admin")]
        public ActionResult Create(Gadget gadget)
        {
            UpdateModel(gadget);
            _gadgetRespository.Save();
            return RedirectToAction("Details", new { gadgetId = gadget.GadgetID });
        }
    }
}
